package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.GeneratorRoom;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.service.GeneratorRoomService;

@Controller
public class GeneratorRoomController {
	
	@Autowired
	private GeneratorRoomService gRoomService;
	
	
	
	@ResponseBody
	@RequestMapping(value="/queryGRoom/{pn}",method=RequestMethod.GET)
	public Msg 	queryGRoomList(GeneratorRoom gRoom,@PathVariable("pn")Integer pn){
		PageHelper.startPage(pn, 5);
		List<GeneratorRoom> list= gRoomService.queryRoomAndDeptWithAdmin(gRoom);
		PageInfo<GeneratorRoom> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
	/**
	 * 获取所有的机房信息，并以分页的方式显示出来
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="roomList",method=RequestMethod.GET)
	public Msg getGRoomList(@RequestParam(value="pn",defaultValue="1")Integer pn){
		PageHelper.startPage(pn, 5);
		List<GeneratorRoom> list= gRoomService.getAllRoomAndDeptWithAdmin();
		PageInfo<GeneratorRoom> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
	/**
	 * 删除一条信息 获取批量删除
	 * @param gRoomIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/gRoom/{gRoomIds}" ,method=RequestMethod.DELETE)
	public Msg deleteUser(@PathVariable(value="gRoomIds")String gRoomIds){
		System.out.println(gRoomIds);
		if(gRoomIds.contains("-")){
			String[] str = gRoomIds.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}
			System.out.println(ids);
			gRoomService.deleteBatch(ids);
		}else{
			gRoomService.deleteDept(Integer.parseInt(gRoomIds));
		}
		return Msg.success();
	}
	
	/**
	 * 更新机房  获取更新机房信息
	 * @param gRoom
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="gRoom",method=RequestMethod.PUT)
	public Msg updateGRoom(GeneratorRoom gRoom){
		gRoomService.updateGRoom(gRoom);
		return Msg.success();
	}
	
	
	/**
	 * 保存机房信息
	 * @param gRoom
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="gRoom",method=RequestMethod.POST)
	public Msg saveGRoom(GeneratorRoom gRoom){
		gRoomService.saveGRoom(gRoom);
		return Msg.success();
	}
	
	/**
	 * 根据机房的id获取机房详细信息
	 * @param gRoom
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="gRoom/{gRoomId}",method=RequestMethod.GET)
	public Msg getGRoomByid(@PathVariable("gRoomId")Integer gRoomId){
		GeneratorRoom gRoom=gRoomService.selectByPrimaryKeyWithDeptAndAdmin(gRoomId);
		return Msg.success().add("gRoom", gRoom);
	}
}
